//
//  MyViewController.m
//  豆瓣HW
//
//  Created by dllo on 15/11/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MyViewController.h"
#import "MyTableViewCell.h"
#import "LoginViewController.h"
#import "DataBaseHandle.h"
#import "User.h"
#import "DetailMineViewController.h"

@interface MyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableV;

@end

@implementation MyViewController

- (void)dealloc
{
    [_tableV release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    User *user = [[DataBaseHandle shareDataBase] selectByState:@"1"];
    if (user.name != nil) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(logoutAction:)];
    } else {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
    }
}

//- (void)viewDidAppear:(BOOL)animated
//{111111
//    User *user = [[DataBaseHandle shareDataBase] selectByState:@"1"];
//    if (user.name != nil) {
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(logoutAction:)];
//    } else {
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
//    }
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的";
    
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];

    
//    User *user = [[DataBaseHandle shareDataBase] selectByState:@"1"];
//    if (user.name != nil) {
//            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(logoutAction:)];
//    } else {
//        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
//    }
    
}

- (void)logoutAction:(UIBarButtonItem *)sender
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认注销" preferredStyle:UIAlertControllerStyleAlert];
    [alertC addAction:[UIAlertAction actionWithTitle: @"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        
    }]];
    [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        User *user = [[DataBaseHandle shareDataBase] selectByState:@"1"];
        user.name = user.name;
        user.password = user.password;
        user.state = @"0";
        [[DataBaseHandle shareDataBase] updateDataWithUser:user];
//        [[DataBaseHandle shareDataBase] closeDB];
//        [[DataBaseHandle shareDataBase] openDB];
        [self.tableV reloadData];
        User *user1 = [[DataBaseHandle shareDataBase] selectByState:@"1"];
        if (user1.name != nil) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(logoutAction:)];
        } else {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
        }
        
    }]];
    [self presentViewController:alertC animated:YES completion:^{
        
        
    }];
}

- (void)barAction:(UIBarButtonItem *)sender
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:YES];
    [loginVC release];
    [self.tableV reloadData];
}

// 选中行触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        
        User *user = [[DataBaseHandle shareDataBase] selectByState:@"1"];
        if (user.name == nil) {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
            [loginVC release];
        } else {
            NSMutableArray *arr = [[DataBaseHandle shareDataBase] selectActivityByName:user.name];
            DetailMineViewController *detailMineVC = [[DetailMineViewController alloc] init];
            detailMineVC.topTitle = @"活动收藏";
            detailMineVC.titleArr = arr;
            [self.navigationController pushViewController:detailMineVC animated:YES];
            [detailMineVC release];
            

            
        }

    }
    if (1 == indexPath.row) {
        
        User *user = [[DataBaseHandle shareDataBase] selectByState:@"1"];
        if (user.name == nil) {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
            [loginVC release];
        } else {
            NSMutableArray *arr = [[DataBaseHandle shareDataBase] selectMovieByName:user.name];
            DetailMineViewController *detailMineVC = [[DetailMineViewController alloc] init];
            detailMineVC.topTitle = @"电影收藏";
            detailMineVC.titleArr = arr;
            [self.navigationController pushViewController:detailMineVC animated:YES];
            [detailMineVC release];
        
        
        }

    }
    if (2 == indexPath.row) {
        User *user = [[DataBaseHandle shareDataBase] selectByState:@"1"];
        if (user.name == nil) {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:loginVC animated:YES];
            [loginVC release];
        } else {
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认清除缓存" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle: @"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
                
            }]];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [[DataBaseHandle shareDataBase] deleteActivityDataWithUserName:user];
                [[DataBaseHandle shareDataBase] deleteMovieDataWithUserName:user];
                
            }]];
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
            
            //            [[DataBaseHandle shareDataBase] deleteActivityDataWithUserName:user];
            //            [[DataBaseHandle shareDataBase] deleteMovieDataWithUserName:user];
            //            NSLog(@"清除完成");
            
        }
        
        
    }
}


// 每个区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

// 铺建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  
    static NSString *cellStr = @"cell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (nil == cell) {
        cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr] autorelease];
    }
    if (0 == indexPath.row) {
        cell.label.text = @"我的活动";
    }
    if (1 == indexPath.row) {
        cell.label.text = @"我的电影";
    }
    if (2 == indexPath.row) {
        cell.label.text = @"清除缓存";
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
